<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\BaseModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('openid');
            $grid->column('user_sn');
            $grid->column('name');
            $grid->avatar->image('', 50);
            $grid->column('sex')->using(\App\Models\User::sexMap());
            $grid->column('birth');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('country');
            $grid->column('is_worker')->switch();
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('openid');
                $filter->equal('user_sn');
                $filter->like('name');
                $filter->equal('sex')->select(\App\Models\User::sexMap());
                $filter->between('birth')->date();
                $filter->like('phone');
                $filter->like('email');
                $filter->equal('is_worker')->select(BaseModel::isMap());
                $filter->equal('status')->select(BaseModel::statusMap());
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'name']);
            // 禁用
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            // 导出
            $grid->export()->rows(function ($rows) {
                $sex = \App\Models\User::sexMap();
                $is = BaseModel::isMap();
                $status = BaseModel::statusMap();
                foreach ($rows as $index => &$row) {
                    $row['sex'] = $sex[$row['sex']];
                    $row['is_worker'] = $is[$row['is_worker']];
                    $row['status'] = $status[$row['status']];
                }

                return $rows;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('openid');
            $show->field('user_sn');
            $show->field('name');
            $show->field('avatar');
            $show->field('sex');
            $show->field('birth');
            $show->field('phone');
            $show->field('email');
            $show->field('email_verified_at');
            $show->field('country');
            $show->field('password');
            $show->field('remember_token');
            $show->field('is_worker');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
            // 禁用
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->display('openid');
            $form->display('user_sn');
            $form->display('name');
            $form->display('avatar');
            $form->display('sex')->options(\App\Models\User::sexMap());
            $form->display('birth');
            $form->display('phone');
            $form->display('email');
            $form->display('email_verified_at');
            $form->display('country');
            $form->text('is_worker')->options(BaseModel::statusMap())->default(BaseModel::NO);
            $form->radio('status')->options(BaseModel::statusMap())->default(BaseModel::STATUS_DISABLE);
        });
    }
}
